<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class FlinkController extends AdminBaseController
{
    const CNAME = '友情链接';

    protected $link_model;

    public function _initialize() {
        parent::_initialize();
        $this->link_model = D("Common/Flink");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 友情链接列表
     */
    public function index() {
        $list = $this->link_model->fetchAll();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加友情链接
     */
    public function add() {
        if (IS_POST) {
            $data = $this->link_model->create();
            if ($data) {
                $id = $this->link_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->link_model->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 编辑友情链接
     */
    public function edit() {
        if (IS_POST) {
            $data = $this->link_model->create();
            if ($data) {
                if ($this->link_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->link_model->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $detail = $this->link_model->find($id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除友情链接
     */
    public function delete() {
        $id = I('post.id', 0, 'intval');

        if (is_numeric($id)) {
            $this->link_model->delete($id);
            $this->success('删除成功', U('index'));
        } elseif (is_array($id)) {
            $this->link_model->delete(implode(',', $id));
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的' . self::CNAME);
        }
    }

    /**
     * 更新排序
     */
    public function updateSort() {

        $status = parent::_updateListSort($this->link_model);
        if ($status) {
            $this->success("更新排序成功", U('index'));
        } else {
            $this->error("更新排序失败");
        }
    }
}